package com.myapp.mobilesafe.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.telephony.SmsMessage;
import android.util.Log;

import com.myapp.mobilesafe.activity.R;
import com.myapp.mobilesafe.service.LocationService;
import com.myapp.mobilesafe.utils.SuperAdmin;

public class SmsReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Object[] objs = (Object[]) intent.getExtras().get("pdus");
        for(Object obj : objs){
            SmsMessage sms = SmsMessage.createFromPdu((byte[])obj);
            String origibatingAddress = sms.getOriginatingAddress();
            String smsBody = sms.getMessageBody();
            Log.i("hahah","@@@##$$@@@"+smsBody+origibatingAddress);
            if("#*alarm*#".equals(smsBody)){
                Log.i("hahah", "已屏蔽");
                MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.alarm);
                mediaPlayer.setVolume(05f,05f);
                mediaPlayer.setLooping(true);
                mediaPlayer.start();
                abortBroadcast();
            }else if("#*location*#".equals(smsBody)){
                Log.i("hahah","进入位置锁定,启动服务");
                context.startService(new Intent(context, LocationService.class));
                abortBroadcast();
            }else if("#*lockscreen*#".equals(smsBody)){
                Log.i("hahah","进入屏幕锁定,启动服务");
                SuperAdmin.init(context);
                SuperAdmin.lockScreen();
                abortBroadcast();
            }
        }
    }
}
